package com.example.demo.controller;

import cn.hutool.json.JSONUtil;
import com.example.demo.config.security.LoginUser;
import com.example.demo.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class TestController {

    @GetMapping("hello")
    public String hello() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String principalStr = JSONUtil.toJsonStr(principal);
        log.info("用户信息：{}",principalStr);
        return "hello:"+JSONUtil.toJsonStr(SecurityContextHolder.getContext().getAuthentication());
    }
    @GetMapping("principal")
    public Object principal(String remark) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String principalStr = JSONUtil.toJsonStr(principal);
        log.info("用户信息：{}",principalStr);
        LoginUser loginUser = (LoginUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return JSONUtil.toJsonStr(loginUser)+remark;
    }

    @GetMapping("hello2")
    public String hello2() {
        return "hello2";
    }

    @GetMapping("user")
    public R user() {
        return R.ok(SecurityContextHolder.getContext().getAuthentication().getPrincipal());
    }

    @GetMapping("successForwardUrl")
    public String successForwardUrl() {
        return "successForwardUrl";
    }

    @GetMapping("defaultSuccessUrl")
    public String defaultSuccessUrl() {
        return "defaultSuccessUrl";
    }

    @GetMapping("failureForwardUrl")
    public String failureForwardUrl() {
        return "failureForwardUrl";
    }

    @GetMapping("failureUrl")
    public String failureUrl() {
        return "failureUrl";
    }
}
